<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="小程序" prop="mpId">
                            <el-select v-model="condition.search.mpId" placeholder="请选择小程序" style="width:200px;">
                                <el-option
                                    v-for="item in mpInfos"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    {{ item.name }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="分类" prop="category">
                            <el-select v-model="condition.search.category"
                                       placeholder="分类">
                                <el-option
                                    v-for="item in category"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="行为" prop="behavior">
                            <el-select v-model="condition.search.behavior"
                                       placeholder="行为">
                                <el-option label="链接"
                                           value="H5"></el-option>
                                <el-option label="小程序"
                                           value="MNP"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            size="small"
            :data="tableData"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="appName"
                label="小程序">
            </el-table-column>
            <el-table-column
                prop="title"
                label="标题">
            </el-table-column>
            <el-table-column
                prop="category"
                :formatter="formatCategory"
                width="110px"
                label="分类">
            </el-table-column>
            <el-table-column
                prop="url"
                width="120px"
                label="地址">
                <template v-slot="scope">
                    <media-show :url="scope.row.url" :preview-src-list="mediaUrls"></media-show>
                </template>
            </el-table-column>
            <el-table-column
                prop="behavior"
                :formatter="formatBehavior"
                label="行为">
            </el-table-column>
            <el-table-column
                prop="description"
                label="描述">
                <template v-slot="scope">
                    <table-popover :text=scope.row.description v-if="scope.row.description"></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="label"
                label="标签">
            </el-table-column>
            <el-table-column
                prop="path"
                label="跳转路径">
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态">
                <template v-slot="scope">
                    <el-switch
                        @change="changeStatus(scope.row)"
                        v-model="scope.row.status"
                        active-color="#13ce66"
                        inactive-color="#ff4949">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="120">
                <template v-slot="scope">
                    <div>
                        <el-button @click="showCopyDialog(scope.row)" type="text" size="small">复制</el-button>
                        <el-button @click="showUpdateDialog(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-drawer ref="drawer" direction="rtl" :title=addOrUpdateDialogTitle width="30%"
                   :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="140px" size="small">
                <el-form-item label="选择小程序">
                    <el-select v-model="addOrUpdateForm.mpId" placeholder="请选择小程序" style="width:200px;">
                        <el-option
                            v-for="item in mpInfos"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="分类">
                    <el-select v-model="addOrUpdateForm.category" @change="selectChanged" clearable
                               placeholder="分类">
                        <el-option
                            v-for="item in category"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="标题">
                    <el-input v-model="addOrUpdateForm.title"
                              placeholder="标题"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="addOrUpdateForm.description" type="textarea"
                              placeholder="描述"></el-input>
                </el-form-item>
                <el-form-item label="行为">
                    <el-select v-model="addOrUpdateForm.behavior" placeholder="行为" clearable>
                        <el-option label="链接" value="H5"></el-option>
                        <el-option label="小程序" value="MNP"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="标签">
                    <el-input v-model="addOrUpdateForm.label" placeholder="标签，多个逗号分隔"></el-input>
                </el-form-item>
                <el-form-item label="跳转路径">
                    <el-input v-model="addOrUpdateForm.path" placeholder="跳转路径"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="addOrUpdateForm.status"></el-switch>
                </el-form-item>
                <el-form-item label="选择图片/视频">
                    <el-button @click="selectAlbumDialogVisible = true" size="small" type="success">
                        选择图片/视频
                    </el-button>
                    <div class="form-tips">分享给朋友，分享到朋友圈，添加到收藏只能选择图片，不支持视频哦。{{ tips }}</div>
                    <div v-if="selectedAlbum.id" style="margin-top: 10px">
                        <media-show :url="selectedAlbum.url"></media-show>
                    </div>
                </el-form-item>
                <el-form-item label="视频是否自动播放" v-if="selectedAlbum.id && selectedAlbum.type === 'video'">
                    <el-switch v-model="addOrUpdateForm.autoplay"></el-switch>
                </el-form-item>
            </el-form>
            <div class="drawer-footer">
                <el-button class="drawer-footer-button" @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button class="drawer-footer-button" @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-drawer>

        <el-dialog title="选择图片" width="70%" :visible.sync="selectAlbumDialogVisible" append-to-body>
            <album ref="albumSelect" size="small" @select="onSelectAlbum" mode="select"></album>
            <div slot="footer" class="dialog-footer"></div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addMnpConfig,
    deleteMnpConfig,
    getMnpConfigByPage,
    getMpInfoByPage,
    getUploadUrl,
    switchMnpConfigStatus,
    updateMnpConfig
} from '@/api/api'
import MediaShow from "@/component/single/MediaShow";
import TablePopover from "@/component/single/TablePopover";
import Album from "@/component/Album";

export default {
    name: "Banner",
    components: {TablePopover, MediaShow, Album},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {
                status: "true"
            },
            // 选中行
            selectedRow: {},
            // 分类
            category: this.CONST.mnp_config_category,
            // 小程序列表
            mpInfos: [],
            // 图片或者视频url数组
            mediaUrls: [],

            // 图片提示信息
            tips: '',

            selectAlbumDialogVisible: false,
            selectedAlbum: [],
        }
    },
    methods: {
        changeStatus(row) {
            switchMnpConfigStatus({id: row.id}).then(res => {
                this.getData();
            })
        },
        selectChanged(value) {
            if (value == 'SHARE_FRIEND') {
                this.tips = '发送给朋友图片长宽比最好是5:4尺寸！'
            } else if (value == 'SHARE_MOMENT') {
                this.tips = '分享到朋友圈图片长宽比最好是1:1尺寸！'
            } else if (value == 'ADD_FAVORITE') {
                this.tips = '添加到收藏图片长宽比最好是1:1尺寸！'
            } else {
                this.tips = '';
            }
        },
        upload() {
            return getUploadUrl();
        },
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        onSelectAlbum(row) {
            this.selectedAlbum = row;
            this.selectAlbumDialogVisible = false;
            this.addOrUpdateForm.url = row.url;
            this.addOrUpdateForm.size = row.size;

            this.$set(this.addOrUpdateForm, "title", row.title);
            this.$set(this.addOrUpdateForm, "description", row.description);
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatCategory: function (row, column, cellValue) {
            if (!cellValue) {
                return "";
            }
            for (const elem of this.category) {
                if (elem.value == cellValue) {
                    return elem.label;
                }
            }
            return "未知";
        },
        formatBehavior: function (row, column, cellValue) {
            if (!cellValue) {
                return "";
            }
            if (cellValue == 'H5') {
                return "链接";
            } else if (cellValue == 'MNP') {
                return "小程序";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
            this.$set(this.addOrUpdateForm, "status", true)
            this.$set(this.addOrUpdateForm, "autoplay", false)
            if (this.condition.search.mpId) {
                this.$set(this.addOrUpdateForm, "mpId", this.condition.search.mpId)
            }
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            this.selectedAlbum = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        showCopyDialog: function (row) {
            this.addOrUpdateDialogTitle = '复制';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
            this.addOrUpdateForm.id = null;
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addMnpConfig(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        update: function () {
            updateMnpConfig(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteMnpConfig({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            } else if (this.addOrUpdateDialogTitle == '复制') {
                this.add();
            }
        },
        // 获取小程序列表
        getMpInfos: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {
                    accountType: 'MINI_PROGRAM'
                }
            };
            getMpInfoByPage(condition).then(data => {
                this.mpInfos = data.body.rows;
            });
        },
        // 获取表格数据
        getData: function () {
            getMnpConfigByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
                this.tableData.forEach((item, index, arr) => {
                    this.mediaUrls.push(item.url);
                })
            });
            this.getMpInfos();
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
